package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.json.JSONArray;
import com.example.emos.api.common.util.JsonToArrayListWithGson;
import com.example.emos.api.common.util.R;
import com.example.emos.api.service.PermissionService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
@Tag(name="PermissionController",description = "权限Web接口")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/searchAllPermission")
    @Operation(summary = "查询所有权限,moduleId --》前端控制菜单用")
//    @SaCheckPermission(value = {"ROOT","ROLE:INSERT","ROLE:UPDATE"},mode = SaMode.OR)
    public R searchAllPermission(){
//        ArrayList<HashMap> list=permissionService.searchAllPermission();
//        JsonObject jsonUser = new JsonObject();
//        jsonUser.addProperty("moduleName",1);
        String jsonString = "[{\"label\":\"用户管理\",\"key\":1,\"children\":[{\"label\":\"纹身师\",\"key\":12},{\"label\":\"商家\",\"key\":13}," +
                "{\"label\":\"C端用户\",\"key\":14}]},{\"label\":\"订单管理\",\"key\":15},{\"label\":\"预约管理\",\"key\":16}," +
                "{\"label\":\"系统管理\",\"key\":2,\"children\":[{\"label\":\"组织架构管理\",\"key\":17},{\"label\":\"角色权限管理\",\"key\":18},{\"label\":\"账号管理\",\"key\":19}," +
                "{\"label\":\"基础资料\",\"key\":3,\"children\":[{\"label\":\"型号管理\",\"key\":20}]},{\"label\":\"固件包\",\"key\":21},{\"label\":\"硬件管理\",\"key\":22}]},{\"label\":\"首页\",\"key\":23}]";

        // 创建 Gson 实例，并注册自定义反序列化器
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Object.class, new JsonToArrayListWithGson.CustomDeserializer())
                .create();

        // 使用 Gson 解析 JSON 字符串
        Type listType = new TypeToken<List<Map<String, Object>>>(){}.getType();
        List<Map<String, Object>> list = gson.fromJson(jsonString, listType);

        return R.ok().put("list",list);
    }
}
